package blog.sys.service;

import blog.sys.dao.ChatMsgDao;
import blog.sys.dao.SysFileDao;
import blog.sys.entity.ChatMsg;
import blog.sys.entity.SysFile;
import blog.web.app.data.ClientInfo;
import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import pub.dao.GeneralDao;
import pub.functions.UrlFuncs;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;

/**
 * Created by Admin on 2016/11/4.
 */
@Service
@Transactional(readOnly = true)
public class ChatMsgService {

    /**
     * 保存消息记录
     *
     * @param chatMsg
     * @return
     */
    @Transactional
    public Integer save(ChatMsg chatMsg)  {

        //将信息保存至数据库
        if (chatMsg.getType() == 2) {// 语音
            SysFile file = new SysFile();
            byte[] imgByteArr = Base64.decodeBase64(chatMsg.getContent());
            file.setContent(imgByteArr);
            file.setContentType("audio/mpeg");
            file.setCreateTime(new Date());
            file.setName(new Date().getTime() + ".mp3");
            file.setRelId(0);
            file.setRelTable("t_chat_msg");
            file.setSeq(0);
            file.setSize(imgByteArr.length);
            file.setType(1);
            sysFileDao.save(file);

            Integer fileId = file.getId();
            chatMsg.setContent("语音");
            chatMsg.setVoiceFileId(fileId);
            chatMsgDao.save(chatMsg);
        }else{
            chatMsgDao.save(chatMsg);
        }
        return chatMsg.getVoiceFileId();
    }

    @Autowired
    private ChatMsgDao chatMsgDao;
    @Autowired
    private SysFileDao sysFileDao;
}
